// 模拟订单数据
const orders = [
  {
    orderId: 'ORD20230001',
    createTime: '2023-06-15 10:30:45',
    totalAmount: 128.5,
    status: 'pending',
    paymentMethod: '支付宝',
    paymentStatus: '已支付',
    paymentTime: '2023-06-15 10:32:10',
    receiverName: '张三',
    receiverPhone: '13800138000',
    receiverAddress: '北京市朝阳区建国路88号',
    remark: '请尽快发货',
    items: [
      {
        goodsId: 'G001',
        goodsName: '商品A',
        price: 59.9,
        quantity: 1
      },
      {
        goodsId: 'G002',
        goodsName: '商品B',
        price: 68.6,
        quantity: 1
      }
    ],
    statusHistory: [
      {
        time: '2023-06-15 10:30:45',
        content: '订单创建',
        type: 'primary'
      },
      {
        time: '2023-06-15 10:32:10',
        content: '支付成功',
        type: 'success'
      }
    ]
  },
  {
    orderId: 'ORD20230002',
    createTime: '2023-06-14 15:20:30',
    totalAmount: 256.0,
    status: 'processing',
    paymentMethod: '微信支付',
    paymentStatus: '已支付',
    paymentTime: '2023-06-14 15:22:05',
    receiverName: '李四',
    receiverPhone: '13900139000',
    receiverAddress: '上海市浦东新区张江高科技园区',
    remark: '',
    items: [
      {
        goodsId: 'G003',
        goodsName: '商品C',
        price: 128.0,
        quantity: 2
      }
    ],
    statusHistory: [
      {
        time: '2023-06-14 15:20:30',
        content: '订单创建',
        type: 'primary'
      },
      {
        time: '2023-06-14 15:22:05',
        content: '支付成功',
        type: 'success'
      },
      {
        time: '2023-06-14 16:05:00',
        content: '订单已确认',
        type: 'warning'
      }
    ]
  },
  {
    orderId: 'ORD20230003',
    createTime: '2023-06-10 09:15:20',
    totalAmount: 599.0,
    status: 'completed',
    paymentMethod: '银行卡',
    paymentStatus: '已支付',
    paymentTime: '2023-06-10 09:18:30',
    receiverName: '王五',
    receiverPhone: '13700137000',
    receiverAddress: '广州市天河区珠江新城',
    remark: '周末送货',
    items: [
      {
        goodsId: 'G004',
        goodsName: '商品D',
        price: 599.0,
        quantity: 1
      }
    ],
    statusHistory: [
      {
        time: '2023-06-10 09:15:20',
        content: '订单创建',
        type: 'primary'
      },
      {
        time: '2023-06-10 09:18:30',
        content: '支付成功',
        type: 'success'
      },
      {
        time: '2023-06-10 10:30:00',
        content: '订单已确认',
        type: 'warning'
      },
      {
        time: '2023-06-12 14:00:00',
        content: '订单已完成',
        type: 'success'
      }
    ]
  }
]

// 模拟API响应
export const mockGetOrders = (params) => {
  const { pageNum = 1, pageSize = 10, orderId, status, startDate, endDate } = params || {}
  
  // 筛选逻辑
  let filteredOrders = [...orders]
  
  if (orderId) {
    filteredOrders = filteredOrders.filter(order => 
      order.orderId.includes(orderId)
    )
  }
  
  if (status) {
    filteredOrders = filteredOrders.filter(order => 
      order.status === status
    )
  }
  
  if (startDate && endDate) {
    filteredOrders = filteredOrders.filter(order => {
      const orderDate = new Date(order.createTime.split(' ')[0])
      const start = new Date(startDate)
      const end = new Date(endDate)
      return orderDate >= start && orderDate <= end
    })
  }
  
  // 分页逻辑
  const total = filteredOrders.length
  const startIndex = (pageNum - 1) * pageSize
  const endIndex = startIndex + pageSize
  const paginatedOrders = filteredOrders.slice(startIndex, endIndex)
  
  return {
    code: 200,
    message: 'success',
    data: {
      list: paginatedOrders,
      total,
      pageNum,
      pageSize
    }
  }
}

export const mockGetOrderDetail = (id) => {
  const order = orders.find(o => o.orderId === id)
  
  if (order) {
    return {
      code: 200,
      message: 'success',
      data: order
    }
  } else {
    return {
      code: 404,
      message: 'Order not found'
    }
  }
}

export const mockUpdateOrderStatus = (id, status, shippingInfo = null) => {
  const order = orders.find(o => o.orderId === id)
  
  if (order) {
    order.status = status
    
    // 如果是发货状态且提供了物流信息
    if (status === 'shipped' && shippingInfo) {
      order.logisticsCompany = shippingInfo.logisticsCompany
      order.trackingNumber = shippingInfo.trackingNumber
      order.shippingRemark = shippingInfo.remark
      
      // 添加发货记录到状态历史
      order.statusHistory.push({
        time: new Date().toISOString().replace('T', ' ').substring(0, 19),
        content: `订单已发货 - ${shippingInfo.logisticsCompany}，单号：${shippingInfo.trackingNumber}`,
        type: 'success'
      })
      
      // 如果有备注，添加备注记录
      if (shippingInfo.remark) {
        order.statusHistory.push({
          time: new Date().toISOString().replace('T', ' ').substring(0, 19),
          content: `发货备注：${shippingInfo.remark}`,
          type: 'info'
        })
      }
    } else {
      // 其他状态更新
      order.statusHistory.push({
        time: new Date().toISOString().replace('T', ' ').substring(0, 19),
        content: `订单状态更新为: ${status}`,
        type: 'info'
      })
    }
    
    return {
      code: 200,
      message: 'success',
      data: order
    }
  } else {
    return {
      code: 404,
      message: 'Order not found'
    }
  }
}

export const mockCancelOrder = (id) => {
  const order = orders.find(o => o.orderId === id)
  
  if (order) {
    order.status = 'cancelled'
    order.statusHistory.push({
      time: new Date().toISOString().replace('T', ' ').substring(0, 19),
      content: '订单已取消',
      type: 'danger'
    })
    return {
      code: 200,
      message: 'success'
    }
  } else {
    return {
      code: 404,
      message: 'Order not found'
    }
  }
}